using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectGioiThieuCongTy()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_GIOITHIEUCONGTY
			};
			return View(CreateViewModel(data));
		}



		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteGioiThieuCongTy(int id)
		{
			var deleteItem = _repGioiThieuCongTy.SelectByID(id);
			var fileName = deleteItem.Logo;
			var result = _repGioiThieuCongTy.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
					DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repGioiThieuCongTy.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfGioiThieuCongTy(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_GIOITHIEUCONGTY,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateGioiThieuCongTy(FormCollection forms)
        {
            string id = forms.GetValues("GioiThieuCongTy_ID")[0];
            string gioithieu = forms.GetValues("GioiThieuCongTy_GioiThieu")[0];
            string gioithieurutgon = forms.GetValues("GioiThieuCongTy_GioiThieuRutGon")[0];
            string diachi = forms.GetValues("GioiThieuCongTy_DiaChi")[0];
            string dienthoai = forms.GetValues("GioiThieuCongTy_DienThoai")[0];
            var file = Request.Files["GioiThieuCongTy_Logo"];
            string refName = "";
            var editItem = _repGioiThieuCongTy.SelectByID(int.Parse(id));
            editItem.GioiThieu = gioithieu;
            editItem.GioiThieuRutGon = gioithieurutgon;
            editItem.DiaChi = diachi;
            editItem.DienThoai = dienthoai;
            var result = false;
            //if (file.ContentLength != 0)
            //{
            //    result = UploadFile(file, "/Content/Images/Items/", id, ref refName);
            //    editItem.Logo = refName;
            //}
            //result = _repGioiThieuCongTy.Save();
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_GIOITHIEUCONGTY,
                IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
                //InfoText = result ? "Item has been updated" : "Cannot update this item"
                InfoText = "Item has been updated"
            };
            return View("DetailOfGioiThieuCongTy", CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertGioiThieuCongTy(FormCollection forms)
        {
            string gioithieu = forms.GetValues("GioiThieuCongTy_GioiThieu")[0];
            string gioithieurutgon = forms.GetValues("GioiThieuCongTy_GioiThieuRutGon")[0];
            string diachi = forms.GetValues("GioiThieuCongTy_DiaChi")[0];
            string dienthoai = forms.GetValues("GioiThieuCongTy_DienThoai")[0];
            var file = Request.Files["GioiThieuCongTy_Logo"];
            string refName = "";
            var newItem = new GioiThieuCongTy
            {
                GioiThieu = gioithieu,
                GioiThieuRutGon = gioithieurutgon,
                DiaChi = diachi,
                DienThoai = dienthoai,
                Logo = "noimages.jpg"
            };
            var result = _repGioiThieuCongTy.Insert(newItem);
            //if (result)
            //{
            //    if (file.ContentLength != 0)
            //    {
            //        result = UploadFile(file, "/Content/Images/Items/", newItem.ID.ToString(), ref refName);
            //        if (result)
            //        {
            //            var addedItem = _repGioiThieuCongTy.SelectByID(newItem.ID);
            //            addedItem.Logo = refName;
            //            _repGioiThieuCongTy.Save();
            //        }
            //    }
            //}
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_GIOITHIEUCONGTY,
                InfoText = result ? "New item has been added" : "Cannot insert new item"
            };
            return View("SelectGioiThieuCongTy", CreateViewModel(data));
        }
	}
}